<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\CustHub\Service;

use App\Common\Constants\StatusConstants;
use App\CustHub\Mapper\ChApiDocMapper;
use Mine\Abstracts\AbstractService;
use function Hyperf\Collection\data_get;

/**
 * APi文档管理服务类
 */
class ChApiDocService extends AbstractService
{
    /**
     * @var ChApiDocMapper
     */
    public $mapper;

    public function __construct(ChApiDocMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * 文档列表
     * @param array $params
     * @return array
     */
    public function getApiDocList(array $params): array
    {
        $list = $this->mapper->model::query()->select(['id', 'article_name'])->where('status', StatusConstants::ENABLE)
            ->orderByRaw('sort asc,created_at desc')
            ->get()->toArray();
        return $list;
    }

    /**
     * 文章详情
     * @param array $params
     * @return array
     */
    public function getApiDocDetail(array $params): array
    {
        $articleId = data_get($params, 'id');
        if (!empty($articleId)) {
            $article = $this->mapper->model::select(['id', 'article_name', 'content', 'view', 'created_at', 'updated_at'])
                ->where('id', $articleId)
                ->where('status', StatusConstants::ENABLE)
                ->first();
            $this->mapper->model::where('id', $articleId)->increment('view');
        } else {
            $article = [];
        }
        return empty($article) ? [] : $article->toArray();
    }
}